<!DOCTYPE html>
<html>
<head>
    <title>Physics Diagram - Sliding Rod</title>
</head>
<body>
<canvas id="physicsCanvas" width="400" height="350"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // Style settings
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.font = 'italic 20px Times New Roman';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';

    // --- Define Geometry ---
    const originX = 60;
    const originY = 300;
    const wallTopY = 40;
    const floorRightX = 380;

    // Rod properties
    const L = 280; // Rod length
    const theta = 35 * Math.PI / 180; // Angle with the vertical wall in radians

    // Calculate endpoint coordinates
    const Ax = originX;
    const Ay = originY - L * Math.cos(theta);
    const Bx = originX + L * Math.sin(theta);
    const By = originY;

    // --- Drawing Functions ---

    // Function to draw wall and floor with hatching
    function drawSurfaces() {
        // Main lines for wall and floor
        ctx.lineWidth = 2;
        ctx.beginPath();
        ctx.moveTo(originX, wallTopY);
        ctx.lineTo(originX, originY);
        ctx.lineTo(floorRightX, originY);
        ctx.stroke();

        // Hatching
        ctx.lineWidth = 1;
        const hatchSpacing = 15;
        const hatchLength = 10;

        // Wall hatching (\ direction)
        for (let y = wallTopY; y < originY + hatchSpacing; y += hatchSpacing) {
            ctx.beginPath();
            ctx.moveTo(originX - hatchLength, y - hatchLength);
            ctx.lineTo(originX, y);
            ctx.stroke();
        }

        // Floor hatching (\ direction)
        for (let x = originX; x < floorRightX + hatchSpacing; x += hatchSpacing) {
            ctx.beginPath();
            ctx.moveTo(x - hatchLength, originY);
            ctx.lineTo(x, originY + hatchLength);
            ctx.stroke();
        }
    }

    // Function to draw the rod AB
    function drawRod() {
        ctx.lineWidth = 6;
        ctx.lineCap = 'butt';
        ctx.beginPath();
        ctx.moveTo(Ax, Ay);
        ctx.lineTo(Bx, By);
        ctx.stroke();
        ctx.lineCap = 'round'; // Reset to default
    }
    
    // Function to draw the collar at midpoint C
    function drawCollarC() {
        const Cx = (Ax + Bx) / 2;
        const Cy = (Ay + By) / 2;

        ctx.save();
        ctx.translate(Cx, Cy);
        // Rod's angle with the horizontal is (PI/2 - theta), clockwise.
        ctx.rotate(-(Math.PI / 2 - theta));
        
        // Collar body
        ctx.strokeStyle = '#3a3a3a';
        ctx.lineWidth = 10;
        ctx.beginPath();
        ctx.moveTo(0, -7);
        ctx.lineTo(0, 7);
        ctx.stroke();
        
        // Highlight dot
        ctx.fillStyle = 'white';
        ctx.beginPath();
        ctx.arc(0, 0, 1.5, 0, 2 * Math.PI);
        ctx.fill();
        
        ctx.restore();
    }


    // Function to draw the velocity vector at B
    function drawVelocityVector() {
        ctx.lineWidth = 2;
        const vLength = 50;
        const arrowheadSize = 8;
        const vEndX = Bx + vLength;
        const vEndY = By;

        // Vector line
        ctx.beginPath();
        ctx.moveTo(Bx, By);
        ctx.lineTo(vEndX, vEndY);
        ctx.stroke();

        // Arrowhead
        ctx.beginPath();
        ctx.moveTo(vEndX, vEndY);
        ctx.lineTo(vEndX - arrowheadSize, vEndY - arrowheadSize / 2);
        ctx.lineTo(vEndX - arrowheadSize, vEndY + arrowheadSize / 2);
        ctx.closePath();
        ctx.fill();
    }

    // Function to draw labels
    function drawLabels() {
        // Label A
        ctx.fillText('A', Ax + 20, Ay);
        
        // Label B
        ctx.fillText('B', Bx + 15, By - 15);
        
        // Label C
        const Cx = (Ax + Bx) / 2;
        const Cy = (Ay + By) / 2;
        ctx.fillText('C', Cx + 15, Cy - 20);

        // Angle theta
        ctx.lineWidth = 1.5;
        const arcRadius = 30;
        ctx.beginPath();
        ctx.arc(Ax, Ay, arcRadius, -Math.PI / 2, -Math.PI / 2 + theta);
        ctx.stroke();
        ctx.fillText('θ', Ax + 20, Ay + 25);

        // Velocity v
        ctx.fillText('v', Bx + 50 + 15, By);
    }

    // --- Main Drawing Execution ---
    drawSurfaces();
    drawRod();
    drawCollarC();
    drawVelocityVector();
    drawLabels();

</script>
</body>
</html>